<?php

namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Model\President;

class PresidentController extends Controller
{
        //
        public function getPresident(Request $request){
            $president = President::orderBy('id')-> get();
            return response([
                'data' => $president,
                'code' => 200
            ]);
        }

        public function addPresident(Request $request){
        $data = $request->data;
        foreach($data['add']??[] as $key => $item){
            President::create([
                'loads' => $item['loads'],
                'old' => $item['old'],
                'make_by' => $item['make_by'],
                'price' => $item['price'],
                'sold_time' => $item['sold_time'],
                'type' => $request->get('type'),
            ]);
        }
        President::destroy(array_pluck($data['remove']??[],'id'));
        foreach($data['update']??[] as $key => $item){
            President::find($item['id'])->update([
                'loads' => $item['loads'],
                'old' => $item['old'],
                'make_by' => $item['make_by'],
                'price' => $item['price'],
                'sold_time' => $item['sold_time'],
                'type' => $request->get('type'),
            ]);
        }

        return response([
            'msg' => '提交成功',
            'code' => 200
        ]);
    }
}
